package com.souyun.serverbloguser.controller;

import com.souyun.serverbloguser.entity.TbUser;
import com.souyun.serverbloguser.mapper.IDbBlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

// http://localhost:8080/user/getUserById?userId=1

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IDbBlogMapper iDbBlogMapper;

    @Autowired
    HttpServletRequest request;

    /** getUserById 根据用户id获取用户信息
     * TODO
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserById")
    @ResponseBody
    public Object getUserById(HttpServletRequest request) {

        try {
            int userId = 0;
            String temp = request.getParameter("userId");
            if (temp != null && !temp.equals("")) {
                try {
                    userId = Integer.parseInt(temp);
                } catch (Exception ignored) {
                }
            }

            TbUser user = iDbBlogMapper.getUserById(userId);
            return user;


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @RequestMapping(value = "/hello")
    public String hello() {

        String token = request.getHeader("token");
        System.out.println(token);

        return "hello, my name is server-blog-user:8082";
    }

}
